package com.springboot.mall.controller;

/*
 *
 *目录控制器
 *@data 2022-05-09
 */

import com.github.pagehelper.PageInfo;
import com.springboot.mall.common.ApiRestResponse;
import com.springboot.mall.common.Constant;
import com.springboot.mall.exception.MallException;
import com.springboot.mall.exception.MallExceptionEnum;
import com.springboot.mall.model.entity.User;
import com.springboot.mall.model.request.AddCategoryRequest;
import com.springboot.mall.model.request.UpdateCategoryRequest;
import com.springboot.mall.model.vo.CategoryVO;
import com.springboot.mall.service.CategoryServcice;
import com.springboot.mall.service.UserService;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.util.List;

@Controller
public class CategoryController {
    @Autowired
    private UserService userService;
    @Autowired
    private CategoryServcice categoryServcice;

    @ApiOperation("新增商品目录")
    @PostMapping("/admin/category/add")
    @ResponseBody
    //传入的参数过多，封装这些参数为一个实体类
    public ApiRestResponse addCategory(HttpSession session,
                                       @Valid  @RequestBody AddCategoryRequest addCategoryRequest) throws MallException {
        //一个一个校验太麻烦了
        /*if (addCategoryRequest.getName() == null || addCategoryRequest.getType() == null
        || addCategoryRequest.getParentId() == null || addCategoryRequest.getOrderNum() == null){
            return ApiRestResponse.error(MallExceptionEnum.PARA_NOT_NULL);
        }*/

        User currentUser = (User)session.getAttribute(Constant.USER);
        if (currentUser  == null){
            return ApiRestResponse.error(MallExceptionEnum.NEED_LOGIN);
        }
        //校验是否是管理员
        boolean adminRole = userService.checkAdminRole(currentUser);
        if (adminRole){
            //是管理员
            categoryServcice.add(addCategoryRequest);
            return ApiRestResponse.success();
        }else{
            return ApiRestResponse.error(MallExceptionEnum.NEED_ADMIN);
        }
    }


    @ApiOperation("商品目录更新")
    @PostMapping("/admin/category/update")
    @ResponseBody
    //传入的参数过多，封装这些参数为一个实体类
    public ApiRestResponse updateCategory(HttpSession session,
                                          @Valid  @RequestBody UpdateCategoryRequest updateCategoryRequest) throws MallException {

        User currentUser = (User)session.getAttribute(Constant.USER);
        if (currentUser  == null){
            return ApiRestResponse.error(MallExceptionEnum.NEED_LOGIN);
        }
        //校验是否是管理员
        boolean adminRole = userService.checkAdminRole(currentUser);
        if (adminRole){
            //是管理员
            categoryServcice.update(updateCategoryRequest);
            return ApiRestResponse.success();
        }else{
            return ApiRestResponse.error(MallExceptionEnum.NEED_ADMIN);
        }
    }

    @ApiOperation("商品目录删除")
    @PostMapping("/admin/category/delete")
    @ResponseBody
    public ApiRestResponse deleteCategory(Integer id) {
        categoryServcice.delete(id);
        return ApiRestResponse.success();
    }

    @ApiOperation("后台目录分页列表")
    @GetMapping("/admin/category/list")
    @ResponseBody
    public ApiRestResponse listCategoryForAdmin(@RequestParam("pageNum") Integer pageNum, @RequestParam("pageSize") Integer pageSize) {
        PageInfo pageInfo = categoryServcice.listForAdmin(pageNum, pageSize);
        return ApiRestResponse.success(pageInfo);
    }

    @ApiOperation("前台目录分页列表")
    @GetMapping("/category/list")
    @ResponseBody
    public ApiRestResponse listCategoryForCustomer() {
        //前台的页面数和页大小由我们自己决定
        List<CategoryVO> categoryVOS = categoryServcice.listForCustomer(0);
        return ApiRestResponse.success(categoryVOS);
    }

}
